package com.bulletelc.apollopre.module.main.center.orders.orderComment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.framework.BaseActivity;
import com.bulletelc.apollopre.module.display.product.details.comment.ImageDetailFragment;
import com.bulletelc.apollopre.widget.HackyViewPager;
import com.bulletelc.apollopre.widget.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片查看器
 */
public class ImagePagerActivity extends BaseActivity {
    private HackyViewPager vpPager;
    private LinearLayout llIndicator;
    private int curPosition; // 当前显示的图片位置
    private List<String> paths; // 图片的地址集合

    @Override
    public int getLayoutRes() {
        return R.layout.image_pager_activity;
    }

    @Override
    public void assignViews() {
        vpPager = findViewById(R.id.vpPager);
        llIndicator = findViewById(R.id.llIndicator);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        curPosition = getIntent().getIntExtra(IntentCode.EXTRA_KEY_IMAGE_CURRENT_POSITION, 0);
        paths = getIntent().getStringArrayListExtra(IntentCode.EXTRA_KEY_IMAGES_PATH);
        vpPager.setAdapter(new ImagePagerAdapter(getSupportFragmentManager(), paths));
        vpPager.setCurrentItem(curPosition);
        final ViewPagerIndicator indicator = new ViewPagerIndicator(this, paths.size(), llIndicator);
        indicator.carouselIndicator(curPosition);
        vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicator.carouselIndicator(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void setFullScreen() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }


    private class ImagePagerAdapter extends FragmentStatePagerAdapter {
        private List<String> paths;

        public ImagePagerAdapter(FragmentManager fm, List<String> paths) {
            super(fm);
            setPaths(paths);
        }

        public void setPaths(List<String> paths) {
            this.paths = paths == null ? new ArrayList<String>() : paths;
        }

        @Override
        public Fragment getItem(int position) {
            return ImageDetailFragment.newInstance(paths.get(position));
        }

        @Override
        public int getCount() {
            return paths == null ? 0 : paths.size();
        }
    }
}
